package presentation;

import java.io.IOException;
import java.util.Calendar;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.hibernate.Session;

import db.DbUtils;


 public class AddCommentServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
	 private static final long serialVersionUID = 1L;
	 
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		request.setCharacterEncoding("UTF-8");
		HttpSession session = request.getSession();
		Long imageId = Long.parseLong(request.getParameter("image"));
		Long userId=(Long)session.getAttribute("userid");
		String content = request.getParameter("content");
		Session hbSession = DbUtils.startSession();
		try {
			Calendar now = Calendar.getInstance();
			System.out.println(now.getTime());
			DbUtils.addComment(hbSession, content, userId, imageId, now.getTime());
			hbSession.getTransaction().commit();
			response.sendRedirect("ViewPicture.jsp?image="+imageId);
		} catch (Exception e) {
			if (hbSession != null) {
				hbSession.getTransaction().rollback();
				hbSession.close();
			}
			response.sendRedirect("Error.jsp?msg=comment");
			e.printStackTrace();
		}
	}  		  	    
}